package 线程池.CompletableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/4/24 13:47
 * @description:
 * @version:
 */
public class ShowThread2 {
    //创建线程池  第一个参数代表核心线程数
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main....start....");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程Id：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return i;
        }, executor);
        //get()：阻塞式等待，只有等线程执行完毕，才能获取到返回值
        Integer integer = future.get();
        System.out.println("多线程返回值为：" + integer);
        System.out.println("main....end....");
    }
}
